/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;


import Daos.UsuarioPetianoDao;
import Model.UsuarioPetiano;
import Util.FormatDate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author alissonrenan
 */
@WebServlet(name = "ServletCadastro", urlPatterns = {"/Cadastro"})
public class ServletCadastro extends HttpServlet {

    private final int CURSO_CIENCIA_COMPUTACAO = 1;
    private final int CURSO_INFORMATICA = 2;
    private final int CURSO_ENG_PRODUCAO = 3;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("cadastroPetiano")!=null) {
            UsuarioPetiano petiano= new UsuarioPetiano();

            petiano.setNome(request.getParameter("nome"));
            petiano.setDataNascimento(FormatDate.stringToDate(request.getParameter("nascimento")));
            petiano.setRa(Integer.parseInt(request.getParameter("ra")));
            petiano.setTelefone(request.getParameter("telefone"));
            petiano.setRg(request.getParameter("rg"));
            petiano.setCpf(request.getParameter("cpf"));
            petiano.setEmail(request.getParameter("email"));

            int opcaoCurso = Integer.parseInt(request.getParameter("curso"));

            if (opcaoCurso == CURSO_CIENCIA_COMPUTACAO){
                petiano.setCurso("Ciencia da Computação");
            } else if (opcaoCurso == CURSO_INFORMATICA){
                petiano.setCurso("Informatica");
            } else if (opcaoCurso == CURSO_ENG_PRODUCAO){
                petiano.setCurso("Engenharia de Produção");
            }

            String senha = request.getParameter("senha");
            petiano.setSenha(Util.Util.encriptaSenha(senha));

            Daos.UsuarioPetianoDao petianoDao = new UsuarioPetianoDao();
            petianoDao.insert(petiano);

            response.sendRedirect("index.jsp");
        }
    }

}
